﻿import { ElNotification, ElMessageBox, ElMessage, ElLoading } from 'element-plus'

function notify(msg, type) {
    type = type || "success"
    ElNotification({ message: msg, type: type })
}

function message(msg, type, option) {
    option = { 
        message: msg, 
        type: type , 
        dangerouslyUseHTMLString:true, 
        showClose:true,
        customClass:"topmsg",
        ...option
    };
    type = type || "success"
    ElMessage(option)
}

function warning(msg) {
    message(msg,"warning")
}

function error(msg) {
    message(msg, "error");
}

function confirm(msg, type, option) {
    type = type || "warning"
    option = {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: type,
        customClass:"topmsg",
        ...option
    };
    let title = option.title || "询问";
    return ElMessageBox.confirm(msg, title, option);
}

function alert(msg, type, option) {
    type = type || "success"
    option = { 
        confirmButtonText: '确定',
        type: type,
        customClass:"topmsg",
        ...option
    }
    let title = option.title || "提示";
    return ElMessageBox.alert(msg, title, option);
}

function prompt(msg, option) {
    option = {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass:"topmsg",
        ...option
    };
    let title = option.title || "提示";
    return new Promise((resolve, reject)=>{
        ElMessageBox.prompt(msg, title, option).then(res=>{
            if(res.action=="confirm"){
                resolve(res.value);
                return
            }
            //reject(res);
        }).catch(()=>{});
    })
}


function showLoading(msg, target) {
    let loading = ElLoading.service({
        fullscreen: false,
        lock: false,
        //text: msg,
        //spinner: 'el-icon-loading',
        target: target || "#app",
    });
    return loading;
}



export default { notify, message, error, warning, alert, confirm, prompt, showLoading }